﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Rison.DataAccess.Common;
using Rison.DBHelper;

namespace Rison.DataAccess.Data.Biz
{
    public sealed class InfomationDb : DataAccessBase
    {
        public InfomationDb()
            : base()
        {
        }

        public InfomationDb(DbBaseHelper dbTransaction)
            : base(dbTransaction)
        {
        }

        protected override string tableName
        {
            get { return "Biz_Infomation"; }
        }

        protected override string IdentityFieldName
        {
            get { return "InfoID"; }
        }

        protected override string OrderBy
        {
            get { return "Biz_Infomation.CreateTime desc"; }
        }

        //protected override string GetSqlFromTable()
        //{
        //    StringBuilder sql = new StringBuilder();
        //    sql.Append("select InfoId,pName,sys_modules.title,Biz_Infomation.moduleId,Biz_Infomation.subject,Biz_Infomation.createDate,");
        //    sql.Append("creator=case when (name is null or 0=len(name)) then userid else name end,");
        //    sql.Append("IsRecommend=case when IsRecommend=1 then '是' else '否' end,DueDate,");
        //    sql.Append("visitedNum=(select count(1) from BZ_InfoVisit v where v.InfoId=Biz_Infomation.InfoId) ");
        //    sql.Append("from Biz_Infomation inner join sys_modules on sys_modules.mid=Biz_Infomation.moduleId ");
        //    sql.Append("inner join sys_product on sys_product.pid=sys_modules.productId ");
        //    sql.Append("left outer join sys_users on sys_users.UID=Biz_Infomation.Creator ");

        //    return sql.ToString();
        //}



        public DataSet GetTopNumInfos(int categoryId, int topNum)
        {
            return GetTopNumInfos(categoryId, topNum, 0);
        }

        public DataSet GetTopNumInfos(int categoryId, int topNum, int subjectLength)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("select top {0} infoId,moduleid,imgUrl,createdate,subject as wholeSubject", topNum.ToString());
            if (subjectLength > 1)
                sql.AppendFormat(",subject=case when (datalength(subject)>{0}*2) then dbo.fn_substringSubject(subject,{1}) else subject end ", (subjectLength).ToString(), (subjectLength - 1).ToString());
            else
                sql.Append(",subject");
            sql.Append(",right(convert(varchar(10),createdate,120),5) as cdate ");
            sql.AppendFormat("from Biz_Infomation where moduleId={0} ", categoryId.ToString());
            sql.Append("order by Biz_Infomation.CreateDate desc");

            return base.ExecuteSql(sql.ToString(), "GetTopNumInfos");
        }

    }
}
